/**
 * Created by wange21 on 2017/7/3.
 */

(function (window, $) {
    "use strict";

    var dom;
    var total;
    var now;
    var maxItem;
    var next;
    var cl;
    var mode;
    var url;

    var page = {};
    var pageText = {};

    var asyn;
    var version = "v0.5.0";
    var sPage = function (obj) {
        return new sPage.fn.init(obj);
    };

    sPage.fn = sPage.prototype = {
        spage: version,
        constructor: sPage
    };

    var init = sPage.fn.init = function (obj) {
        dom = obj.dom;
        now = obj.now ? obj.now : 1;
        maxItem = obj.maxItem ? obj.maxItem : 5;
        total = obj.total ? obj.total : 1;
        cl = obj.cl ? obj.cl : "sPage";
        mode = obj.mode ? obj.mode : 1;
        url = obj.url ? obj.url : null;
        asyn = !!obj.asyn;

        if (!obj.pageText) {
            obj.pageText = {};
        }
        pageText = {
            first: obj.pageText.first ? obj.pageText.first : "First",
            prev: obj.pageText.prev ? obj.pageText.prev : "Prev",
            next: obj.pageText.next ? obj.pageText.next : "Next",
            last: obj.pageText.last ? obj.pageText.last : "Last"
        };

        page = {
            first: '<li><a class="first">' + pageText.first + '</a></li>',
            prev: '<li><a class="prev">' + pageText.prev + '</a></li>',
            com: '<li><a class="com"></a></li>',
            next: '<li><a class="next">' + pageText.next + '</a></li>',
            last: '<li><a class="last">' + pageText.last + '</a></li>'
        };

        return sPage;
    };

    sPage.prototype.init.prototype = sPage.prototype;

    sPage.mode = {
        simple: function () {
            dom.html(page.prev + page.next);
            if (now === 1) {
                dom.children().eq(0).addClass("disabled");
            }
            if (now === total || total === 0) {
                dom.children().last().addClass("disabled");
            }
        },

        simple2: function () {
            dom.html(page.first + page.prev + page.next + page.last);
            if (now === 1) {
                dom.children().eq(0).addClass("disabled");
                dom.children().eq(1).addClass("disabled");
            }
            if (now === total || total === 0) {
                dom.children().last().addClass("disabled");
                dom.children().last().prev().addClass("disabled");
            }
        },

        normal: function () {
            dom.html(page.first + page.prev);
            if (total < maxItem) {
                //页数少于最大页数
                //The pages are less than the maximum page number
                for (var i = 1; i <= total; i++) {
                    dom.append(page.com);
                    dom.children().last().children().text(i);
                }
                dom.children().eq(now + 1).addClass("disabled").addClass(
                    'active');
            } else if (total - now < maxItem) {
                //剩余页数少于最大页数
                //The remaining pages are less than the maximum number of pages
                for (i = total - maxItem + 1; i <= total; i++) {
                    dom.append(page.com);
                    dom.children().last().children().text(i);
                }
                dom.children().eq(now - total + maxItem + 1).addClass("disabled")
                    .addClass('active');
            } else {
                //剩余页数大于等于最大页数
                //The remaining pages are greater than or equal to the maximum number of pages
                for (i = now; i < now + maxItem; i++) {
                    dom.append(page.com);
                    dom.children().last().children().text(i);
                }
                dom.children().eq(2).addClass("disabled").addClass('active');
            }
            dom.append(page.next + page.last);
            if (now === 1) {
                dom.children().eq(0).addClass("disabled");
                dom.children().eq(1).addClass("disabled");
            }
            if (now === total || total === 0) {
                dom.children().last().addClass("disabled");
                dom.children().last().prev().addClass("disabled");
            }
        }
    };

    sPage.asyn = function (fun) {
        if (!dom || !asyn) {
            return sPage;
        }

        dom.delegate("li","click",function () {
            if ($(this).hasClass("disabled")) {
                return;
            }
            var d = $(this).children().eq(0);
            if (d.hasClass("com")) {
                now = parseInt(d.text());
            } else if (d.hasClass("first")) {
                now =1 ;
            } else if (d.hasClass("prev")) {
                now -= 1;
            } else if (d.hasClass("next")) {
                now +=1;
            } else if (d.hasClass("last")) {
                now = total;
            }
            fun();
            sPage.gen();
        });

        return sPage;
    };

    sPage.gen = function () {
        if (!dom) {
            return;
        }
        switch (mode) {
            case 1:
                sPage.mode.simple();
                break;
            case 2:
                sPage.mode.simple2();
                break;
            case 3:
                sPage.mode.normal();
                break;
        }

        if (url) {
            dom.children().each(function () {
                if ($(this).hasClass("disabled")) {
                    return;
                }
                var d = $(this).children().eq(0);
                if (d.hasClass("com")) {
                    d.attr("href", url + "=" + d.text());
                } else if (d.hasClass("first")) {
                    d.attr("href", url + "=1");
                } else if (d.hasClass("prev")) {
                    d.attr("href", url + "=" + (sPage.getnow() - 1));
                } else if (d.hasClass("next")) {
                    d.attr("href", url + "=" + (sPage.getnow() + 1));
                } else if (d.hasClass("last")) {
                    d.attr("href", url + "=" + total);
                }
            });
        }

        return sPage;
    };

    sPage.getnow = function () {
        return now;
    };

    sPage.getRequest = function () {
        var url = location.search;
        var theRequest = {};
        if (url.indexOf("?") !== -1) {
            var str = url.substr(1);
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = strs[i].split("=")[1];
            }
        }
        return theRequest;
    };

    window.sPage = sPage;
})(window, jQuery);
